﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace VideoClassLibrary
{
    // <summary>
    // 桑慧娜
    // </summary>
    public partial class VDataBase
    {
        public static Microsoft.Practices.EnterpriseLibrary.Data.Database CreateDatabase()
        {
            Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("VideoConnString");
            return db;
        }
        public string constr = ConfigurationManager.ConnectionStrings["VideoConnString"].ConnectionString;
        public SqlDataReader GetDataByReader(string sql, bool IsProc, params SqlParameter[] pList)
        {
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(sql, con);
            if (IsProc)
                cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in pList)
            {
                cmd.Parameters.Add(p);
            }
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            return reader;
        }
        public DataSet GetDataByds(string sql, string TName, bool IsProc, params SqlParameter[] pList)
        {
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(sql, con);
            if (IsProc)
                cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in pList)
            {
                cmd.Parameters.Add(p);
            }
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, TName);
            return ds;
        }
        public object GetDataByScalar(string sql, bool IsProc, params SqlParameter[] pList)
        {
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(sql, con);
            if (IsProc)
                cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in pList)
            {
                cmd.Parameters.Add(p);
            }

            con.Open();
            object result = cmd.ExecuteScalar();
            con.Close();
            return result;
        }
        public int GetDataByNonQuery(string sql, bool IsProc, params SqlParameter[] pList)
        {
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(sql, con);
            if (IsProc)
                cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in pList)
            {
                cmd.Parameters.Add(p);
            }
            con.Open();
            int result = cmd.ExecuteNonQuery();
            con.Close();
            return result;
        }
     
    }
}
